package com.ruoyi.finance.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.finance.service.IReciveReqService;
import com.ruoyi.system.api.domain.finance.ReciveReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * 应收需求单Controller
 *
 * @author wl
 * @date 2024-11-01
 */
@RestController
@RequestMapping("/reciveReq")
@Slf4j
public class ReciveReqController extends BaseController {
    @Autowired
    private IReciveReqService reciveReqService;

    /**
     * 查询应收需求单列表
     */
    @RequiresPermissions("finance:reciveReq:list")
    @GetMapping("/list")
    public TableDataInfo list(ReciveReq reciveReq) {
        log.info("ReciveReqController list reciveReq:{}", reciveReq);
        startPage();
        List<ReciveReq> list = reciveReqService.selectReciveReqList(reciveReq);
        TableDataInfo tableDataInfo = getDataTable(list);
        log.info("ReciveReqController list tableDataInfo:{}", tableDataInfo);
        return tableDataInfo;
    }

    /**
     * @return com.ruoyi.common.core.web.page.TableDataInfo
     * @description 查询所有应收需求单列表
     * @date 2024/11/15 上午10:30
     * @Param reciveReq:
     **/
    @RequiresPermissions("finance:reciveReq:list")
    @GetMapping("/listAll")
    public TableDataInfo listAll(ReciveReq reciveReq) {
        log.info("ReciveReqController listAll reciveReq:{}", reciveReq);
        List<ReciveReq> list = reciveReqService.selectReciveReqList(reciveReq);
        TableDataInfo tableDataInfo = getDataTable(list);
        log.info("ReciveReqController listAll tableDataInfo:{}", tableDataInfo);
        return tableDataInfo;
    }

    /**
     * 导出应收需求单列表
     */
    @RequiresPermissions("finance:reciveReq:export")
    @Log(title = "应收需求单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReciveReq reciveReq) {
        log.info("ReciveReqController export reciveReq:{}", reciveReq);
        List<ReciveReq> list = reciveReqService.selectReciveReqList(reciveReq);
        ExcelUtil<ReciveReq> util = new ExcelUtil<ReciveReq>(ReciveReq.class);
        util.exportExcel(response, list, "应收需求单数据");
    }

    /**
     * 获取应收需求单详细信息
     */
    @RequiresPermissions("finance:reciveReq:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        log.info("ReciveReqController getInfo id:{}", id);
        ReciveReq reciveReq = reciveReqService.selectReciveReqById(id);
        log.info("ReciveReqController getInfo reciveReq:{}", reciveReq);
        return success(reciveReq);
    }

    /**
     * 查询当前所有未核销金额总值
     */
    @RequiresPermissions("finance:reciveReq:query")
    @GetMapping(value = "getAllAmount")
    public AjaxResult getAllAmount() {
        BigDecimal cost = reciveReqService.getAllAmount();
        log.info("ReciveReqController getAllAmount cost:{}", cost);
        return success(cost);
    }

    /**
     * 查询当天销售应收笔数及金额
     */
    @RequiresPermissions("finance:reciveReq:query")
    @GetMapping("getSaleOrderReciveCountAndMoney")
    public AjaxResult getSaleOrderReciveCountAndMoney() {
        return success(reciveReqService.getSaleOrderReciveCountAndMoney());
    }

    /**
     * 查询当天采购退货应收笔数及金额
     */
    @RequiresPermissions("finance:reciveReq:query")
    @GetMapping("getBuyBackReciveCountAndMoney")
    public AjaxResult getBuyBackReciveCountAndMoney() {
        return success(reciveReqService.getBuyBackReciveCountAndMoney());
    }
    /**
     * 查询应收单各阶段订单数
     */
    @RequiresPermissions("finance:reciveReq:query")
    @GetMapping( "getReceiveReqThreeCountToIndex")
    public AjaxResult getReceiveReqThreeCountToIndex() {
        return success(reciveReqService.getReceiveReqThreeCountToIndex());
    }
    /**
     * @return com.ruoyi.common.core.domain.R<com.ruoyi.system.api.domain.finance.ReciveReq>
     * @description 根据财务应收单编号获取详情
     * @date 2024/11/10 下午11:09
     * @Param orderCode:
     **/
    @GetMapping("/info/{orderCode}")
    public R<ReciveReq> getReciveReqByOrderCode(@PathVariable("orderCode") String orderCode) {
        log.info("ReciveReqController getReciveReqByOrderCode orderCode:{}", orderCode);
        ReciveReq reciveReq = reciveReqService.getReciveReqByOrderCode(orderCode);
        log.info("ReciveReqController getReciveReqByOrderCode reciveReq:{}", reciveReq);
        return R.ok(reciveReq);
    }

    /**
     * 新增应收需求单
     */
    @RequiresPermissions("finance:reciveReq:add")
    @Log(title = "应收需求单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReciveReq reciveReq) {
        log.info("ReciveReqController add reciveReq:{}", reciveReq);
        AjaxResult ajaxResult = reciveReqService.insertReciveReq(reciveReq);
        log.info("ReciveReqController add ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 修改应收需求单
     */
    @RequiresPermissions("finance:reciveReq:edit")
    @Log(title = "应收需求单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReciveReq reciveReq) {
        log.info("ReciveReqController edit reciveReq:{}", reciveReq);
        AjaxResult ajaxResult = reciveReqService.updateReciveReq(reciveReq);
        log.info("ReciveReqController edit ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 审核后修改财务应收单状态
     * @date 2024/11/10 下午10:31
     * @Param reciveReq:
     **/
    @PostMapping("/updateStatus")
    public AjaxResult updateReciveStatus(@RequestBody ReciveReq reciveReq) {
        log.info("ReciveReqController updateReciveStatus reciveReq:{}", reciveReq);
        AjaxResult ajaxResult = reciveReqService.updateReciveStatus(reciveReq);
        log.info("ReciveReqController updateReciveStatus ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * 删除应收需求单
     */
    @RequiresPermissions("finance:reciveReq:remove")
    @Log(title = "应收需求单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        log.info("ReciveReqController remove ids:{}", Arrays.toString(ids));
        AjaxResult ajaxResult = reciveReqService.deleteReciveReqByIds(ids);
        log.info("ReciveReqController remove ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }


    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 校验应收单编号
     * @date 2024/11/1 上午9:49
     * @Param orderCode:
     **/
    @GetMapping("/check/{orderCode}")
    public AjaxResult checkReciveReqOrderCode(@PathVariable String orderCode) {
        log.info("ReciveReqController checkReciveReqOrderCode orderCode:{}", orderCode);
        AjaxResult ajaxResult = reciveReqService.checkReciveReqOrderCode(orderCode);
        log.info("ReciveReqController checkReciveReqOrderCode ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }

    /**
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     * @description 根据应收单编号修改，sql语句修改已核销金额
     * @date 2024/11/19 上午9:20
     * @Param reciveReq:
     **/
    @PostMapping("/updateReciveOrderByOrderCodeOfVerMoney")
    public AjaxResult updateReciveOrderByOrderCodeOfVerMoney(@RequestBody ReciveReq reciveReq) {
        log.info("ReciveReqController updateReciveOrderByOrderCodeOfVerMoney reciveReq:{}", reciveReq);
        AjaxResult ajaxResult = reciveReqService.updateReciveOrderByOrderCodeOfVerMoney(reciveReq);
        log.info("ReciveReqController updateReciveOrderByOrderCodeOfVerMoney ajaxResult:{}", ajaxResult);
        return ajaxResult;
    }
}
